<?php

namespace services\rbac;

use common\components\Service;
use common\models\rbac\UserRole;

/**
 * Class UserRoleService
 * @package services\rbac
 */
class UserRoleService extends Service
{
    public function findInfo(array $condition = []): ?UserRole
    {
        $query = UserRole::find();

        isset($condition['member_id']) && $query->andWhere(['member_id' => $condition['member_id']]);
        isset($condition['role_id']) && $query->andWhere(['role_id' => $condition['role_id']]);

        return $query->one();
    }

    /**
     * 获取列表
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        if (empty($condition)) {
            return [];
        }

        $query = UserRole::find();

        isset($condition['select']) && $query->select($condition['select']);

        isset($condition['member_id']) && $query->andWhere(['member_id' => $condition['member_id']]);
        isset($condition['role_id']) && $query->andWhere(['role_id' => $condition['role_id']]);
        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);

        return $query->asArray()->all();
    }

    /**
     * 提取顾问id
     * @param $data
     * @return array
     * @author chenyuebin
     */
    public function getMemberList($data)
    {
        $member_list = [];
        foreach ($data as $value) {
            $member_list[] = $value['member_id'];
        }

        return $member_list;
    }
}
